<?php


namespace app\controller;


use app\middleware\Check;
use app\model\BackStoreModel;
use think\facade\Validate;
use think\Request;
use think\response\Json;
use think\validate\ValidateRule;


class BackStore
{
    protected $middleware = [Check::class];

    /**列表
     * @param Request $request
     * @return Json
     */
    public function list(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['limit'] = $request->post('limit');//每页显示数量
        $input['page'] = $request->post('page');//当前页
        $input['name'] = $request->post('name');
        $input['cate_name'] = $request->post('cate_name');
        $input['is_show'] = $request->post('is_show');
        // 参数校验
        $validate = Validate::rule([
            'limit' => ValidateRule::isRequire(null, '缺少参数'),
            'page' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => function ($value) {
                return $value === null ? '缺少参数' : true;
            },
            'cate_name' => function ($value) {
                return $value === null ? '缺少参数' : true;
            },
            'is_show' => function ($value) {
                return in_array($value,['','0','1'], true) ? true : '缺少参数';
            }
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return BackStoreModel::list($input, $request->post('token'));
    }

    /**修改商品上下架|是否售罄
     * @param Request $request
     * @return Json
     */
    public function changeGoodsStatus(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        $input['name'] = $request->post('name');
        $input['status'] = $request->post('status');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => function ($value) {
                return in_array($value, ['is_show', 'is_out'], true) ? true : '非法参数' ;
            },
            'status' => function ($value) {
                return in_array($value, ['0', '1'], true) ? true : '非法参数' ;
            }
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return BackStoreModel::changeGoodsStatus($input, $request->post('token'));
    }
    /**批量下架商品
     * @param Request $request
     * @return Json
     */
    public function multiOff(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['ids'] = $request->post('ids');
        // 参数校验
        $validate = Validate::rule([
            'ids' =>  ValidateRule::isRequire(null, '缺少参数'),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return BackStoreModel::multiOff($input, $request->post('token'));
    }
    /**获取商品分类列表
     * @param Request $request
     * @return Json
     */
    public function findCateList(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        return BackStoreModel::findCateList($request->post('token'));
    }

    /**添加商品
     * @param Request $request
     * @return Json
     */
    public function addGoods(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['stock'] = $request->post('stock');
        $input['price'] = $request->post('price');
        $input['cate_id'] = $request->post('cate_id');
        $input['pic'] = $request->post('pic');
        $input['name'] = $request->post('name');
        $input['detail'] = $request->post('detail');
        // 参数校验
        $validate = Validate::rule([
            'stock' => ValidateRule::isRequire(null, '缺少参数'),
            'price' => ValidateRule::isRequire(null, '缺少参数'),
            'cate_id' => function ($value) {
                return count(explode('-', $value))  === 2 ? true : '不合法数据';
            },
            'pic' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => ValidateRule::isRequire(null, '缺少参数'),
            'detail' => ValidateRule::isRequire(null, '缺少参数'),
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return BackStoreModel::addGoods($input, $request->post('token'));
    }

    /**编辑商品可售数量
     * @param Request $request
     * @return Json
     */
    public function editNum(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        $input['name'] = $request->post('name');
        $input['num'] = $request->post('num');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => ValidateRule::isRequire(null, '缺少参数'),
            'num' => ValidateRule::isRequire(null, '缺少参数'),
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return BackStoreModel::editNum($input, $request->post('token'));
    }


    /**获取单个商品信息
     * @param Request $request
     * @return Json
     */
    public function goodsOnly(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return BackStoreModel::goodsOnly($input, $request->post('token'));
    }

    /**编辑商品
     * @param Request $request
     * @return Json
     */
    public function editGoods(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');//财务id
        $input['stock'] = $request->post('stock');
        $input['price'] = $request->post('price');
        $input['cate_id'] = $request->post('cate_id');
        $input['pic'] = $request->post('pic');
        $input['name'] = $request->post('name');
        $input['detail'] = $request->post('detail');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
            'stock' => ValidateRule::isRequire(null, '缺少参数'),
            'price' => ValidateRule::isRequire(null, '缺少参数'),
            'cate_id' => function ($value) {
                return count(explode('-', $value))  === 2 ? true : '不合法数据';
            },
            'pic' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => ValidateRule::isRequire(null, '缺少参数'),
            'detail' => ValidateRule::isRequire(null, '缺少参数'),
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return BackStoreModel::editGoods($input, $request->post('token'));
    }

    /**删除商品
     * @param Request $request
     * @return Json
     */
    public function deleteGoods(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');//财务id
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return BackStoreModel::deleteGoods($input, $request->post('token'));
    }

    /**获取销售列表
     * @param Request $request
     * @return Json
     */
    public function getSellList(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['limit'] = $request->post('limit','10');//每页显示数量
        $input['page'] = $request->post('page','1');//当前页
        $input['name'] = $request->post('name','');
        $input['start'] = $request->post('start');
        $input['end'] = $request->post('end');
        // 参数校验
        $validate = Validate::rule([
            'limit' => ValidateRule::isRequire(null, '缺少参数'),
            'page' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => function ($value) {
                return $value === null ? '缺少参数' : true;
            },
            'start' => function ($value) {
                return $value === null ? '缺少参数' : true;
            },
            'end' => function ($value) {
                return $value === null ? '缺少参数' : true;
            }
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return BackStoreModel::getSellList($input, $request->post('token'));
    }

    /**获取订单列表
     * @param Request $request
     * @return Json
     */
    public function getOrderList(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['limit'] = $request->post('limit');//每页显示数量
        $input['page'] = $request->post('page');//当前页
        $input['start'] = $request->post('start');
        $input['end'] = $request->post('end');
        $input['status'] = $request->post('status');
        $input['is_pay'] = $request->post('is_pay');
        $input['financial_status'] = $request->post('financial_status');
        // 参数校验
        $validate = Validate::rule([
            'limit' => ValidateRule::isRequire(null, '缺少参数'),
            'page' => ValidateRule::isRequire(null, '缺少参数'),
            'status' => function ($value) {
                if ($value === null) {
                    return '缺少参数';
                } else if (in_array($value, ['','0','1','2','3','4','5','6','7','-1','-2','-3','-4'], true)) {
                    return true;
                } else {
                    return '不合法参数';
                }
            },
            'is_pay' => function ($value) {
                if ($value === null) {
                    return '缺少参数';
                } else if (in_array($value, ['','0','1'], true)) {
                    return true;
                } else {
                    return '不合法参数';
                }
            },
            'financial_status' => function ($value) {
                if ($value === null) {
                    return '缺少参数';
                } else if (in_array($value, ['','0','1'], true)) {
                    return true;
                } else {
                    return '不合法参数';
                }
            },
            'start' => function ($value) {
                return $value === null ? '缺少参数' : true;
            },
            'end' => function ($value) {
                return $value === null ? '缺少参数' : true;
            }
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return BackStoreModel::getOrderList($input, $request->post('token'));
    }

    /**商家财务获取订单列表
     * @param Request $request
     * @return Json
     */
    public function getFinanceOrderList(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['limit'] = $request->post('limit');//每页显示数量
        $input['page'] = $request->post('page');//当前页
        $input['start'] = $request->post('start');
        $input['end'] = $request->post('end');
//        $input['status'] = $request->post('status');
        // 参数校验
        $validate = Validate::rule([
            'limit' => ValidateRule::isRequire(null, '缺少参数'),
            'page' => ValidateRule::isRequire(null, '缺少参数'),
//            'status' => function ($value) {
//                if ($value === null) {
//                    return '缺少参数';
//                } else if (in_array($value, ['','6','7','-3'], true)) {
//                    return true;
//                } else {
//                    return '不合法参数';
//                }
//            },
            'start' => function ($value) {
                return $value === null ? '缺少参数' : true;
            },
            'end' => function ($value) {
                return $value === null ? '缺少参数' : true;
            }
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return BackStoreModel::getFinanceOrderList($input, $request->post('token'));
    }

    /**学校财务获取订单列表
     * @param Request $request
     * @return Json
     */
    public function getSchFinanceOrderList(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['limit'] = $request->post('limit');//每页显示数量
        $input['page'] = $request->post('page');//当前页
        $input['start'] = $request->post('start');
        $input['end'] = $request->post('end');
//        $input['status'] = $request->post('status');
        $input['name'] = $request->post('name');
        // 参数校验
        $validate = Validate::rule([
            'limit' => ValidateRule::isRequire(null, '缺少参数'),
            'page' => ValidateRule::isRequire(null, '缺少参数'),
//            'status' => function ($value) {
//                if ($value === null) {
//                    return '缺少参数';
//                } else if (in_array($value, ['','6','7','-3'], true)) {
//                    return true;
//                } else {
//                    return '不合法参数';
//                }
//            },
            'name' => function ($value) {
                return $value === null ? '缺少参数' : true;
            },
            'start' => function ($value) {
                return $value === null ? '缺少参数' : true;
            },
            'end' => function ($value) {
                return $value === null ? '缺少参数' : true;
            }
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return BackStoreModel::getSchFinanceOrderList($input, $request->post('token'));
    }
}